using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using SAE.Controls;

namespace SAE
{
    public partial class Default : Form
    {
        public Default()
        {
            InitializeComponent();

            if (SPFarm.Local != null)
            {
                this.panelMain.Controls.Clear();
                ObjectViewer webApplicationObjectView = new ObjectViewer();
                webApplicationObjectView.AddingTreeViewNode += new EventHandler(objectView_AddingTreeViewNode);
                webApplicationObjectView.Dock = DockStyle.Fill;
                webApplicationObjectView.TreeView.ImageList = this.imageList;
                webApplicationObjectView.LoadObjectNode(null, SPFarm.Local);
                this.panelMain.Controls.Add(webApplicationObjectView);

                SettingConstants.FarmConnectionString = SPFarm.Local.PairConnectionString;
            }
        }

        private void EnsureFarmConnection(bool alwaysShowOpenDialog)
        {
            if (!(SettingConstants.FarmConnectionString != null && SettingConstants.FarmConnectionString.Trim().Length > 0)
               || alwaysShowOpenDialog)
            {
                Microsoft.Data.ConnectionUI.DataConnectionDialog dcd = new Microsoft.Data.ConnectionUI.DataConnectionDialog();
                dcd.DataSources.Add(Microsoft.Data.ConnectionUI.DataSource.SqlDataSource);
                dcd.SelectedDataProvider = Microsoft.Data.ConnectionUI.DataProvider.SqlDataProvider;
              
                if (!string.IsNullOrEmpty(SettingConstants.FarmConnectionString))
                {
                    dcd.ConnectionString = SettingConstants.FarmConnectionString;
                }

                if (Microsoft.Data.ConnectionUI.DataConnectionDialog.Show(dcd) == DialogResult.OK)
                {
                    SettingConstants.FarmConnectionString = dcd.ConnectionString;
                }
            }            
        }

        private void EnsureSiteUrl(bool alwaysShowOpenDialog)
        {
            if (!(SettingConstants.SiteUrl != null && SettingConstants.SiteUrl.Trim().Length > 0)
                || alwaysShowOpenDialog)
            {
                Forms.OpenSiteDialog openSiteDialog = new Forms.OpenSiteDialog();
                openSiteDialog.URL = SettingConstants.SiteUrl;

                if (openSiteDialog.ShowDialog() == DialogResult.OK)
                {
                    using (SPSite site = new SPSite(openSiteDialog.URL))
                    {
                        SettingConstants.SiteUrl = site.Url;
                    }
                }
            }
        }

        private void EnsureWebUrl(bool alwaysShowOpenDialog)
        {
            if (!(SettingConstants.SiteUrl != null && SettingConstants.SiteUrl.Trim().Length > 0)
                || alwaysShowOpenDialog)
            {
                Forms.OpenSiteDialog openSiteDialog = new Forms.OpenSiteDialog();
                openSiteDialog.URL = SettingConstants.SiteUrl + SettingConstants.WebUrl;

                if (openSiteDialog.ShowDialog() == DialogResult.OK)
                {
                    using (SPSite site = new SPSite(openSiteDialog.URL))
                    {
                        string webUrl = openSiteDialog.URL.Replace(site.Url, string.Empty);
                        SettingConstants.SiteUrl = openSiteDialog.URL;
                        SettingConstants.WebUrl = webUrl;
                    }
                }
            }
        }

        void objectView_AddingTreeViewNode(object sender, EventArgs e)
        {
            if (sender != null && sender.GetType() ==typeof(TreeNode))
            {
                TreeNode node = sender as TreeNode;

                if (node.Tag != null)
                {
                    if (typeof(SPFarm).IsInstanceOfType(node.Tag))
                    {
                        node.ImageKey = "SMT_ICON.gif";
                        node.SelectedImageKey = "SMT_ICON.gif";
                    }
                    if (typeof(SPServer).IsInstanceOfType(node.Tag) || typeof(SPServerCollection).IsInstanceOfType(node.Tag))
                    {
                        node.ImageKey = "servers.gif";
                        node.SelectedImageKey = "servers.gif";
                    }
                    if (typeof(SPService).IsInstanceOfType(node.Tag) || typeof(SPServiceCollection).IsInstanceOfType(node.Tag) || typeof(SPServiceInstance).IsInstanceOfType(node.Tag) || typeof(SPServiceInstanceCollection).IsInstanceOfType(node.Tag))
                    {
                        node.ImageKey = "services.gif";
                        node.SelectedImageKey = "services.gif";
                    }
                    if (typeof(SPWebApplication).IsInstanceOfType(node.Tag) || typeof(SPWebApplicationCollection).IsInstanceOfType(node.Tag))
                    {
                        node.ImageKey = "CSWEB.gif";
                        node.SelectedImageKey = "CSWEB.gif";
                    }
                    if (typeof(SPSite).IsInstanceOfType(node.Tag) || typeof(SPSiteCollection).IsInstanceOfType(node.Tag))
                    {
                        node.ImageKey = "cat.gif";
                        node.SelectedImageKey = "cat.gif";
                    }
                    if (typeof(SPWeb).IsInstanceOfType(node.Tag) || typeof(SPWebCollection).IsInstanceOfType(node.Tag))
                    {
                        node.ImageKey = "STSICON.gif";
                        node.SelectedImageKey = "STSICON.gif";
                    }
                    if (typeof(SPFeature).IsInstanceOfType(node.Tag) || typeof(SPFeatureCollection).IsInstanceOfType(node.Tag) || typeof(SPFeatureDefinition).IsInstanceOfType(node.Tag) || typeof(SPFeatureDefinitionCollection).IsInstanceOfType(node.Tag))
                    {
                        node.ImageKey = "genericfeature.gif";
                        node.SelectedImageKey = "genericfeature.gif";
                    }
                    if (typeof(SPList).IsInstanceOfType(node.Tag) || typeof(SPListCollection).IsInstanceOfType(node.Tag))
                    {
                        node.ImageKey = "LTGEN.gif";
                        node.SelectedImageKey = "LTGEN.gif";
                    }
                    if (typeof(SPDocumentLibrary).IsInstanceOfType(node.Tag))
                    {
                        node.ImageKey = "doclibrary.gif";
                        node.SelectedImageKey = "doclibrary.gif";
                    }
                    if (typeof(SPFolder).IsInstanceOfType(node.Tag) || typeof(SPFolderCollection).IsInstanceOfType(node.Tag))
                    {
                        node.ImageKey = "Folder.gif";
                        node.SelectedImageKey = "Folder.gif";
                    }
                    if (typeof(System.Collections.Hashtable).IsInstanceOfType(node.Tag))
                    {
                        node.ImageKey = "DATA.gif";
                        node.SelectedImageKey = "DATA.gif";
                    }
                    if (typeof(SPJobDefinition).IsInstanceOfType(node.Tag) || typeof(SPJobDefinitionCollection).IsInstanceOfType(node.Tag))
                    {
                        node.ImageKey = "OneTimeJob.gif";
                        node.SelectedImageKey = "OneTimeJob.gif";
                    }
                    if (typeof(SPRunningJob).IsInstanceOfType(node.Tag) || typeof(SPRunningJobCollection).IsInstanceOfType(node.Tag))
                    {
                        node.ImageKey = "QDJob.gif";
                        node.SelectedImageKey = "QDJob.gif";
                    }
                    if (typeof(SPUser).IsInstanceOfType(node.Tag) || typeof(SPUserCollection).IsInstanceOfType(node.Tag))
                    {
                        node.ImageKey = "PERUSR.gif";
                        node.SelectedImageKey = "PERUSR.gif";
                    } 
                    if (typeof(SPGroup).IsInstanceOfType(node.Tag) || typeof(SPGroupCollection).IsInstanceOfType(node.Tag))
                    {
                        node.ImageKey = "ALLUSR.gif";
                        node.SelectedImageKey = "ALLUSR.gif";
                    }
                    if (typeof(SPPermission).IsInstanceOfType(node.Tag) || typeof(SPPermissionCollection).IsInstanceOfType(node.Tag) || typeof(SPPermissionGroup).IsInstanceOfType(node.Tag))
                    {
                        node.ImageKey = "TABSEC.gif";
                        node.SelectedImageKey = "TABSEC.gif";
                    }
                    if (typeof(Microsoft.SharePoint.Workflow.SPWorkflowManager).IsInstanceOfType(node.Tag) || typeof(Microsoft.SharePoint.Workflow.SPWorkflowAssociation).IsInstanceOfType(node.Tag) || typeof(Microsoft.SharePoint.Workflow.SPWorkflowAssociationCollection).IsInstanceOfType(node.Tag) || typeof(Microsoft.SharePoint.Workflow.SPWorkflowCollection).IsInstanceOfType(node.Tag))
                    {
                        node.ImageKey = "availableworkflow.gif";
                        node.SelectedImageKey = "availableworkflow.gif";
                    }
                    if (typeof(SPRecycleBinItem).IsInstanceOfType(node.Tag) || typeof(SPRecycleBinItemCollection).IsInstanceOfType(node.Tag))
                    {
                        node.ImageKey = "RECYCBIN.gif";
                        node.SelectedImageKey = "RECYCBIN.gif";
                    }
                    if (typeof(SPRole).IsInstanceOfType(node.Tag) || typeof(SPRoleCollection).IsInstanceOfType(node.Tag))
                    {
                        node.ImageKey = "ULS16.gif";
                        node.SelectedImageKey = "ULS16.gif";
                    }

                    if (typeof(SPAlternateUrl).IsInstanceOfType(node.Tag) || typeof(SPAlternateUrlCollection).IsInstanceOfType(node.Tag) || typeof(SPAlternateUrlCollectionManager).IsInstanceOfType(node.Tag) || typeof(Uri).IsInstanceOfType(node.Tag))
                    {
                        node.ImageKey = "link.gif";
                        node.SelectedImageKey = "link.gif";
                    }

                    if (typeof(SPDatabase).IsInstanceOfType(node.Tag) || typeof(SPDatabaseCollection).IsInstanceOfType(node.Tag) || typeof(SPContentDatabase).IsInstanceOfType(node.Tag) || typeof(SPContentDatabaseCollection).IsInstanceOfType(node.Tag))
                    {
                        node.ImageKey = "MAPPED.gif";
                        node.SelectedImageKey = "MAPPED.gif";
                    }
                    

                    if (node.Tag.GetType()==typeof(SPFeature))
                    {
                        SPFeature feature = node.Tag as SPFeature;
                        node.Text = (feature.Definition==null?feature.DefinitionId.ToString():feature.Definition.DisplayName);
                    }

                    if (node.Tag.GetType() == typeof(SPRunningJob))
                    {
                        SPRunningJob job = node.Tag as SPRunningJob;
                        node.Text = (job.JobDefinitionTitle != null ? job.JobDefinitionTitle : job.JobDefinition.DisplayName);
                    }

                    if (node.Tag.GetType() == typeof(UserProfile))
                    {
                        object name = (node.Tag as UserProfile)[PropertyConstants.PreferredName].Value;

                        if (name != null && name.ToString().Length>0)
                        {
                            node.Text = name.ToString();
                        }
                    }
                }
            }
        }
   
        private void aboutMenuItem_Click(object sender, EventArgs e)
        {
            new Forms.AboutDialog().ShowDialog();
        }

        private void openFarmMenuItem_Click(object sender, EventArgs e)
        {
            this.EnsureWebUrl(false);

            SPFarm farm=SPFarm.Open(SettingConstants.FarmConnectionString);

            this.panelMain.Controls.Clear();
            ObjectViewer webApplicationObjectView = new ObjectViewer();
            webApplicationObjectView.AddingTreeViewNode += new EventHandler(objectView_AddingTreeViewNode);
            webApplicationObjectView.Dock = DockStyle.Fill;
            webApplicationObjectView.TreeView.ImageList = this.imageList;
            webApplicationObjectView.LoadObjectNode(null, farm);
            this.panelMain.Controls.Add(webApplicationObjectView);
        }

        private void openSiteMenuItem_Click(object sender, EventArgs e)
        {
            this.EnsureSiteUrl(true);

            using (SPSite site = new SPSite(SettingConstants.SiteUrl))
            {
                this.panelMain.Controls.Clear();
                ObjectViewer webApplicationObjectView = new ObjectViewer();
                webApplicationObjectView.AddingTreeViewNode += new EventHandler(objectView_AddingTreeViewNode);
                webApplicationObjectView.Dock = DockStyle.Fill;
                webApplicationObjectView.TreeView.ImageList = this.imageList;
                webApplicationObjectView.LoadObjectNode(null, site);
                this.panelMain.Controls.Add(webApplicationObjectView);
            }
        }

        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void openUserProfileMenuItem_Click(object sender, EventArgs e)
        {
            this.EnsureSiteUrl(true);

            using (SPSite site = new SPSite(SettingConstants.SiteUrl))
            {
                ServerContext sc = ServerContext.GetContext(site);
                UserProfileManager upm = new UserProfileManager(sc);

                this.panelMain.Controls.Clear();
                ObjectViewer webApplicationObjectView = new ObjectViewer();
                webApplicationObjectView.AddingTreeViewNode += new EventHandler(objectView_AddingTreeViewNode);
                webApplicationObjectView.Dock = DockStyle.Fill;
                webApplicationObjectView.TreeView.ImageList = this.imageList;
                webApplicationObjectView.LoadObjectNode(null, upm);
                this.panelMain.Controls.Add(webApplicationObjectView);
            }
        }

        private void openWebMenuItem_Click(object sender, EventArgs e)
        {
            this.EnsureWebUrl(true);

            using (SPSite site = new SPSite(SettingConstants.SiteUrl))
            {
                using (SPWeb web = site.OpenWeb(SettingConstants.WebUrl))
                {
                    this.panelMain.Controls.Clear();
                    ObjectViewer webApplicationObjectView = new ObjectViewer();
                    webApplicationObjectView.AddingTreeViewNode += new EventHandler(objectView_AddingTreeViewNode);
                    webApplicationObjectView.Dock = DockStyle.Fill;
                    webApplicationObjectView.TreeView.ImageList = this.imageList;
                    webApplicationObjectView.LoadObjectNode(null, web);
                    this.panelMain.Controls.Add(webApplicationObjectView);
                }
            }          
        }

        private void runQueryMenuItem_Click(object sender, EventArgs e)
        {
            this.EnsureWebUrl(false);

            this.panelMain.Controls.Clear();
            RunQuery dqc = new RunQuery();
            dqc.Dock = DockStyle.Fill;
            this.panelMain.Controls.Add(dqc);
        }

        private void runAdminJobsMenuItem_Click(object sender, EventArgs e)
        {
            this.EnsureSiteUrl(false);

            this.panelMain.Controls.Clear();
            RunAdminJobs rj = new RunAdminJobs();
            rj.Dock = DockStyle.Fill;
            this.panelMain.Controls.Add(rj);
        }

        private void resetSiteUrlMenuItem_Click(object sender, EventArgs e)
        {
            this.EnsureSiteUrl(true);
        }

        private void resetWebUrlMenuItem_Click(object sender, EventArgs e)
        {
            this.EnsureWebUrl(true);
        }

        private void resetFarmConnectionMenuItem_Click(object sender, EventArgs e)
        {
            this.EnsureFarmConnection(true);
        }       
    }
}